package com.tanhua.server.controller;

import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    @Autowired
    private TanhuaService tanhuaService;


    @GetMapping
    public ResponseEntity users(Long userID, @RequestHeader("Authorization") String token) {
        log.info("userID==" + userID);
        if (userID == null) {
            userID = UserHolder.getUserId();
        }

        UserInfoVo userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }

    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {

        //2、获取id
        Long id = UserHolder.getUserId();
        //设置id
        userInfo.setId(Long.valueOf(id));
        userInfoService.updateById(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) throws IOException {
        Long userId = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto, userId);
        return ResponseEntity.ok(null);
    }
    //获取修改手机号的验证码
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(@RequestHeader("Authorization") String token) {
        String mobile = UserHolder.getMobile();
        userService.sendMsg(mobile);
        return ResponseEntity.ok(null);
    }
    //验证码校验
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestHeader("Authorization") String token,@RequestBody Map map){
        String mobile = UserHolder.getMobile();
        //String code = (String) claims.get("verificationCode");
        String code = (String) map.get("verificationCode");

        Map retMap = userService.updatePhone(mobile,code);
        //3、构造返回
        return ResponseEntity.ok(retMap);
    }
    //保存修改的手机号
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestHeader("Authorization") String token,@RequestBody Map map){
        String phone = (String) map.get("phone");
        User user=new User();
        Long userId = UserHolder.getUserId();
        user.setMobile(phone);
        user.setId(userId);
        userService.saveUpdatePhone(user);
        return ResponseEntity.ok(null);
    }

    //相互喜欢,喜欢数量
    @GetMapping("/counts")
    public ResponseEntity likeCounts(){
        Map map=userService.likeCounts();
        return ResponseEntity.ok(map);
    }

    //喜欢,相互喜欢,粉丝,列表
    @GetMapping("/friends/{type}")
    public ResponseEntity friendsList(@RequestParam(defaultValue = "1") int page,
                                      @RequestParam(defaultValue = "10") int pagesize,
                                      String nickname,
                                      @PathVariable String type){
        PageResult pr=userService.friendsList(page,pagesize,nickname,type);
        return ResponseEntity.ok(pr);
    }
    //取消喜欢
    @DeleteMapping("like/{uid}")
    public ResponseEntity deleteLike(@PathVariable Long uid){
        userService.deleteLike(uid);
        return ResponseEntity.ok(null);
    }
    //首页喜欢
    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable Long uid){
        Boolean flag=userService.love(uid);
        return ResponseEntity.ok(flag);
    }
}
